// pages/classification/classification.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 左边数据
    recommendStatusStrleft: [],
    // 右边数据
    recommendStatusStrrigh: [],
    // 切换
    flag: 0,
    show: false,
    Jumpdetails: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onHide() {
    app.globalData.index = 0,
      app.globalData.id = null
  },
  onShow() {
    this.setData({
      flag: app.globalData.index
    })
    if (app.globalData.index || app.globalData.id) {
      wx.request({
        url: `https://api.it120.cc/zbt/shop/goods/list/v2?categoryId=${app.globalData.id}`,
        method: "POST",
        success: res => {
          console.log(res);
          if (res.data.code == 700) {
            this.setData({
              recommendStatusStrrigh: []
            })
          } else {
            this.setData({
              recommendStatusStrrigh: res.data.data.result
            })
          }
          wx.hideLoading()
        }
      })
    } else {
      wx.request({
        url: `https://api.it120.cc/zbt/shop/goods/category/all`,
        method: "POST",
        success: res => {
          // console.log(res);
          if (res.data.code == 700) {
            this.setData({
              recommendStatusStrrigh: []
            })
          } else {
            this.setData({
              recommendStatusStrleft: res.data.data.result
            })
          }

          wx.hideLoading()
        }

      })
    }
  },
  onLoad: function (e) {


    // 左边数据
    this.classificationleft()
    // 右边数据
    this.classificationright()
  },
  // 左边数据
  classificationleft() {
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: `https://api.it120.cc/zbt/shop/goods/category/all`,
      method: "POST",
      success: res => {
        // console.log(res);
        this.setData({
          recommendStatusStrleft: res.data.data
        })
        wx.hideLoading()
      }

    })

  },
  classificationright() {
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: `https://api.it120.cc/zbt/shop/goods/list/v2`,
      method: "POST",
      success: res => {
        // console.log(res);
        this.setData({
          recommendStatusStrrigh: res.data.data.result
        })
        wx.hideLoading()
      }
    })
  },
  // 右边数据
  click(e) {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      flag: e.target.dataset.index
    })
    wx.request({
      url: `https://api.it120.cc/zbt/shop/goods/list/v2?categoryId=${e.currentTarget.dataset.id}`,
      method: "POST",
      success: res => {
        // console.log(res);
        if (res.data.code == 700) {
          this.setData({
            recommendStatusStrrigh: []
          })
        } else {
          this.setData({
            recommendStatusStrrigh: res.data.data.result
          })
        }
        wx.hideLoading()
      }
    })
  },
  // 搜索
  search() {
    // console.log(1);
    wx.navigateTo({
      url: '/pages/SearCh/SearCh',
    })
  },
  // 加入购物车
  addtocart(e) {

    console.log(e.currentTarget.dataset.item);
    let token = wx.getStorageSync('token') || []
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/add`,
      method: "POST",
      data: {
        goodsId: e.currentTarget.dataset.item.id,
        number: 1,
        token: token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        // console.log(res);
        wx.showLoading({
          title: '添加成功',
        })
        wx.hideLoading()
      }
    })
  },
  increase(id){
    this.setData({
      show: true
    })
    wx.request({
      url: `https://api.it120.cc/zbt/shop/goods/detail`,
      method: "get",
      data: {
        id: id.currentTarget.dataset.id,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        // console.log(res);
        this.setData({
          Jumpdetails: res.data.data.basicInfo,
          properties: res.data.data.properties
        })
      }
    })
  },
  return() {
    this.setData({
      show: false
    })
  },

  hide() {
    this.setData({
      show: false
    })
  },
  // 弹出层加入购物车
  addtocartAsda(e) {
    // console.log(e);
    let token = wx.getStorageSync('token') || []
    wx.request({
      url: `https://api.it120.cc/zbt/shopping-cart/add`,
      method: "POST",
      data: {
        goodsId: e.currentTarget.dataset.item.id,
        number: e.currentTarget.dataset.item.numberGoodReputation,
        token: token,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        // this.ShoppingCartdata()
        if (res.statusCode == 200) {
          this.setData({
            show: false
          })
          wx.showLoading({
            title: '添加成功',
          })
        }
        wx.hideLoading()
      }
    })
  },
  // 详情页
  Jumpdetails(e) {
    //这个是传递id
    console.log(e.currentTarget.dataset.id);
    //跳转的代码
    wx.navigateTo({
      url: `/pages/Jumpdetails/Jumpdetails?id=${e.currentTarget.dataset.id}`,
    });

  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})